<?php

namespace app\model;

use think\Model;

class User extends Model
{
    function getchildren($uid){
        $son = $this->field('nickname,id')->where('sourceid',$uid)->order('addtime', 'desc')->select();
        if($son->isEmpty()){
            return ['son'=>[],'grandson'=>[]];
        }
        $son_ids=[];
        foreach($son as $value){
            array_push($son_ids,$value['id']);
        }
        $grandson = $this->field('nickname,id')->whereIn('sourceid',$son_ids)->order('addtime', 'desc')->select();
        return ['son'=>$son,'grandson'=>$grandson];
    }
    function memberCount(){
        return $this->count();
    }

    static function register($param){
        $access_token=newToken();
        $six_token = substr($access_token,0,6);
        $data['phone']=$param['phone'];
        $data['password']=md5($param['password']);
        $data['access_token']=$access_token;
        $data['addtime']=time();
        $data['nickname']='游客'.$six_token;
        $data['avatar']='https://avatar.tobi.sh/'.$six_token.'?size=64';
        if(isset($param['sourceid']) ){
            $data['sourceid']=$param['sourceid'];
        }
        return self::create($data);
    }
    
}
